package com.socks.library.klog;


import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;

/**
 * Created by zhaokaiqiang on 15/11/18.
 */
public class FileLog {

    private static final String FILE_PREFIX = "KLog_";
    private static final String FILE_FORMAT = ".log";

    public static void printFile(String tag, File targetDirectory, @Nullable String fileName, String headString, String msg) {
        HiLogLabel label  = new HiLogLabel(HiLog.LOG_APP, 0x00201, tag);
        fileName = (fileName == null) ? getFileName() : fileName;
        if (save(targetDirectory, fileName, msg)) {
            try {
                System.out.println(tag+" save log success ! location is >>>" + targetDirectory.getCanonicalPath() + "/" + fileName);
                HiLog.info(label,headString + " save log success ! location is >>>" + targetDirectory.getCanonicalPath() + "/" + fileName);
            } catch (IOException e) {
                System.out.println(tag+" save log fails !"+e.toString());
                e.printStackTrace();
            }
        } else {
            System.out.println(tag+" save log fails !");
            HiLog.info(label,headString + "save log fails !");
        }
    }

    private static boolean save(File dic, @NonNls String fileName, String msg) {

       File file = new File(dic, fileName);
        OutputStream outputStream=null;
        OutputStreamWriter outputStreamWriter=null;
        try {
             outputStream = new FileOutputStream(file);
             outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
            outputStreamWriter.write(msg);
            outputStreamWriter.flush();
            outputStreamWriter.close();
            outputStream.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (outputStreamWriter!=null) {
                    outputStreamWriter.close();
                }
                if (outputStream!=null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;

    }

    private static String getFileName() {
        SecureRandom random = new SecureRandom();
        return FILE_PREFIX + Long.toString(System.currentTimeMillis() + random.nextInt(10000)).substring(4) + FILE_FORMAT;
    }

}
